import 'dart:ffi';

import 'package:flutter/material.dart';

class FormDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Theme(
        data: Theme.of(context).copyWith(
          primaryColor:Colors.black
        ), 
        child: Container(
          padding: EdgeInsets.all(20),
          child:  Column(
             mainAxisAlignment: MainAxisAlignment.center,
             children: <Widget>[
               RegisterFormDemo()
             ],
          ),

        )
        ),
    );
  }
}


class RegisterFormDemo extends StatefulWidget {
  @override
  RegisterFormDemoState createState() => RegisterFormDemoState();
}

class RegisterFormDemoState extends State<RegisterFormDemo> {
  
  final registerFormKey = GlobalKey<FormState>();

  String username,password;

  bool autovalidate = false;
    
    void submitMessage () {
      if( registerFormKey.currentState.validate()){
         registerFormKey.currentState.save();
         debugPrint('username:$username');
         debugPrint('password:$password');
        
        Scaffold.of(context).showSnackBar(SnackBar(content: Text('Registering.....')));

      }else{
        setState(() {
           autovalidate = true;
           });
      }
    }
     String validateUsername (value) {

       if(value.isEmpty){
          return 'Username is error';
       }
           return null;

     }
     String validatePassword (value) {

       if(value.isEmpty){
          return 'Password is error';
       }
           return null;

     }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: registerFormKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(
              labelText: 'Username',
              helperText: ''
            ),
            onSaved: (value){
               username = value;
            },
            validator: validateUsername,
            autovalidate: autovalidate,
          ),
           TextFormField(
             obscureText: true,
             decoration: InputDecoration(
              labelText: 'Password',
              helperText: ''
            ),
            onSaved: (value){
               password = value;
            },
            validator: validatePassword,
            autovalidate: autovalidate,
          ),
          SizedBox(height:10.0),     
          Container(
            width: double.infinity,
            child: RaisedButton(
             color: Colors.green,
             child: Text('Register',style: TextStyle(color: Colors.red)),
             elevation: 0.0,
             onPressed:submitMessage,
            ),
          )
        ],
      ),
       

      
    );
  }
}

class TextFieldDemo  extends StatefulWidget {
  @override
  TextFieldDemoState createState() => TextFieldDemoState();
}

class TextFieldDemoState extends State<TextFieldDemo> {
final textEditingController  = TextEditingController();
@override
  void dispose() {
    // TODO: implement dispose
    textEditingController.dispose();
    super.dispose();
  }
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    textEditingController.addListener(() {
      debugPrint('${textEditingController.text}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: textEditingController,
      // onChanged: (value){
      //   debugPrint('$value');
      // },
      onSubmitted: (value){
        debugPrint('$value');
      },
      decoration: InputDecoration(
        icon: Icon(Icons.subject),
        labelText: 'Title',
        hintText: 'Enter the post title.',
        filled: true
      ),
    );
  }
}



class ThemeDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color:Theme.of(context).accentColor
    );
  }
}